package com.student.demo.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.demo.db.entity.LuTestStudent;
import com.student.demo.db.mapper.LuTestStudentMapper;
import com.student.demo.service.LuTestStudentService;
import com.student.demo.util.BaseException;
import com.student.demo.util.JsonResult;
import com.student.demo.util.PageParam;
import com.student.demo.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LuTestStudentServiceImpl implements LuTestStudentService {

    @Autowired
    private LuTestStudentMapper luTestStudentMapper;

    //redis 引入
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public JsonResult<?> add(LuTestStudent luTestStudent) {
        /**
         *  除字符串外需用ObjectUtils 判断是否为null
         */
        if (ObjectUtil.isEmpty(luTestStudent)){
            throw new BaseException("传入参数异常");
        }
        /**
         *
         * 字符串需用 StringUtils isBlank进行判断  字符串为 ""或null 都不允许 isEmpty 不判断 ""
         */
        if (StringUtils.isBlank(luTestStudent.getName())){
            throw new BaseException("名称不能为空");
        }
        /**
         * 根据数据库设置的长度大小进行限制 注意字符占用空间大小
         */
        if (luTestStudent.getName().length() > 50){
            throw new BaseException("名称不能大于50个字符");
        }
        /**
         * 创建日期需后台set 不需要传值 本次测试日期序列化
         */
        if (ObjectUtil.isEmpty(luTestStudent.getCreateTime())){
            throw new BaseException("日期不能为空");
        }
        //Integer类型  类型通常需要前端传  本次测试
        luTestStudent.setType(1);
        // byte类型 set时 需要强转
        luTestStudent.setStatus((byte)1);
        /**
         * 添加、修改、删除时 需接收int 进行判断是否成功
         */
        int result = luTestStudentMapper.insertSelective(luTestStudent);
        if (result <= 0){
            throw new BaseException("添加失败");
        }
        //Redis String数据类型 存入
        redisTemplate.opsForValue().set("ceshi","测试");
        //Redis 获取
        String str = redisTemplate.opsForValue().get("ceshi");
        System.out.println(str);
        return new JsonResult<>("添加成功");
    }

    @Override
    public JsonResult<?> select(Long id) {
        if (ObjectUtil.isEmpty(id)){
            throw new BaseException("id不能为空");
        }
        LuTestStudent luTestStudent = luTestStudentMapper.selectByPrimaryKey(id);
        /**
         * 根据业务需求 如果查询不到是 返回错误信息或不需要判断 直接返回null
         */
        if (ObjectUtil.isEmpty(luTestStudent)){
            throw new BaseException("数据不存在");
        }
        return new JsonResult(luTestStudent);
    }

    @Override
    public PageResult selectPage(PageParam pageParam) {
        //设置分页条件 page页码 limit 每页条数
        PageHelper.startPage(pageParam.getPage(),pageParam.getLimit());
        //获取模糊搜索字段
        String search = pageParam.getSearchLike();
        //获取type字段 PageParam类继承 HashMap get可直接获取字段
        //Convert.to..  获取字段值 如果获取不到 则默认为null 防止null异常
        Integer type = Convert.toInt(pageParam.get("type"));
        //根据条件查询
        List<LuTestStudent> list = luTestStudentMapper.selectPage(search,type);
        //分页数据
        PageInfo<LuTestStudent> info = new PageInfo<>(list);
        //实例化返回类
        PageResult pageResult = new PageResult();
        //设置总条数
        pageResult.setCount(Convert.toInt(info.getTotal()));
        //设置返回数据
        pageResult.setData(list);
        return pageResult;
    }
}
